home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #1 / Amiga Plus 1995 #1.iso / fish-disketten / fish_841-850 / d846 / iobject / demo.c < prev    next >
C/C++ Source or Header  |  1994-12-13  |  7KB  |  330 lines

  1. /* Demo of IObject.lib
  2.  * Thu Feb  4 12:18:55 1993
  3.  */
  4.  
  5.  
  6. #define  INTUITION_PREFERENCES_H 0
  7. #include <intuition/intuition.h>
  8. #include "IObject.h"
  9.  
  10. /* I use only ROM font so i don't need to open the diskfont.library so i put
  11.  DiskfontBase to NULL */
  12. CPTR DiskfontBase=NULL;
  13.  
  14. struct Window *window=NULL;
  15.  
  16. struct NewWindow new_window=
  17. {
  18.     50, 25,      /* Position of window */
  19.     480, 125,    /* Size of window */
  20.     0, 1,
  21.     CLOSEWINDOW|IDCMP_CYCLE|IDCMP_CHECK|IDCMP_STRING|IDCMP_BUTTON|IDCMP_STRING|
  22.     IDCMP_INTEGER|IDCMP_SCROLLER|VANILLAKEY,   /* IDCMPFlags */
  23.     SMART_REFRESH|WINDOWCLOSE|WINDOWDRAG|WINDOWDEPTH|ACTIVATE|RMBTRAP,    /* Flags*/
  24.     NULL,
  25.     NULL,
  26.     "Test de IObject.lib", /* Title of window */
  27.     NULL,
  28.     NULL,
  29.     0, 0,
  30.     0, 0,
  31.     WBENCHSCREEN   /* Type: Workbench window. */
  32. };
  33.  
  34.  
  35. struct TextAttr font=
  36. {
  37.     "topaz.font",
  38.     8,
  39.     FS_NORMAL,
  40.     FPF_ROMFONT
  41. };
  42.  
  43.  
  44. STRPTR text[]=    /* string array for cycle */
  45. {
  46.     "IObject.lib",
  47.     "is very",
  48.     "useful.",
  49.     NULL    /* NOTEZ-BIEN: you MUST put a NULL at the of the string array */
  50. };
  51.  
  52. struct NewCycle newcycle=
  53. {
  54.     203, 17,
  55.     122, 14,
  56.     LABEL_LEFT|LABEL_HOT_KEY,
  57.     0,
  58.     "C_ycle",
  59.     &font,
  60.     text        /* string array */
  61. };
  62.  
  63. struct NewScroller newscroller=
  64. {
  65.     358, 28,
  66.     106, 11,
  67.     LABEL_ABOVE,
  68.     0,
  69.     "Scroller",
  70.     &font,
  71.     0,        /* Pos = 0 */
  72.     25,        /* visible 25 */
  73.     100        /* Maximum 100 */
  74. };
  75.  
  76.  
  77. struct NewButton newbutton=
  78. {
  79.     22, 17,
  80.     94, 14,
  81.     LABEL_INSIDE|LABEL_HOT_KEY,
  82.     0,
  83.     "_Button",
  84.     &font
  85. };
  86.  
  87.  
  88. struct NewButton newtoggle=
  89. {
  90.     331, 97,
  91.     140, 14,
  92.     LABEL_INSIDE|LABEL_HOT_KEY|LABEL_PEN2,
  93.     BT_TOGGLE,        /* button is a switch */
  94.     "_Toggle Button",
  95.     &font
  96. };
  97.  
  98.  
  99. struct NewCheck newcheck=
  100. {
  101.     403, 74,
  102.     20, 16,
  103.     LABEL_LEFT|LABEL_HOT_KEY,
  104.     0,
  105.     "_Check",
  106.     &font
  107. };
  108.  
  109.  
  110. struct NewInteger newint=
  111. {
  112.     256, 42,
  113.     72, 14,
  114.     LABEL_RIGHT|LABEL_HOT_KEY,
  115.     STR_CENTER,        /* integer centered */
  116.     "_Integer",
  117.     &font
  118. };
  119.  
  120. struct NewString newstr=
  121. {
  122.     8, 38,
  123.     162, 12,
  124.     LABEL_RIGHT|LABEL_HOT_KEY,
  125.     STR_SINGLEBORD,        /* single Border */
  126.     "_String",
  127.     &font,
  128.     "Welcome to IObject.lib",        /* initial text */
  129.     40        /* 40 characters maximum */
  130. };
  131.  
  132. /* ID for the IObject */
  133. #define ID_CYCLE        0
  134. #define ID_SCROLLER    1
  135. #define ID_BUTTON        2
  136. #define ID_CHECK        3
  137. #define ID_INTEGER    4
  138. #define ID_STRING        5
  139. #define ID_TGBUTTON    6
  140.  
  141. #define ID_MIN            ID_CYCLE
  142. #define ID_MAX            ID_TGBUTTON
  143.  
  144. CPTR EZGadget[ID_MAX + 1]={0};        /* IObject array */
  145.  
  146. struct NewTextArea newtextarea=
  147. {
  148.     10, 64,
  149.     312, 57,
  150.     LABEL_ABOVE,
  151.     TAF_CLIP_TEXT,    /* if the text is larger than the TextArea, jump to next line */
  152.     "TextArea",
  153.     &font,
  154.     1,3,
  155.     1,            /* a pixel between two lines */
  156.     0x03        /* bitplanes 1 and 2 used */
  157. };
  158.  
  159. CPTR textarea=NULL;        /* pointer to TextAera */
  160.  
  161. UBYTE HotKeyTable[ID_MAX+1];    /* array for hot keys */
  162.  
  163.  
  164. /*
  165.  * Look at a char in a char array
  166.  * Returns index of char in the array or -1 if not in the array
  167. */
  168. WORD FindHotKey(UBYTE hotkey, UBYTE *table, UWORD len)
  169. {
  170.     UWORD ind=0;
  171.  
  172.     while (len--)
  173.     {
  174.         /* toupper if you don't worry about upcase or lowcase */
  175.         if (toupper(*table++) == toupper(hotkey))
  176.             return (ind);
  177.         ind++;
  178.     }
  179.     return (-1);
  180. }
  181.  
  182.  
  183. /*
  184.  * Free ressources
  185. */
  186. VOID clean_exit(void)
  187. {
  188.     COUNT i;
  189.  
  190.     if (window) CloseWindow(window);
  191.     for (i = ID_MIN; i <= ID_MAX; i++)        /* free all the objects */
  192.         if (EZGadget[i])
  193.             FreeObject(EZGadget[i]);
  194.  
  195.     if (textarea)    FreeTextArea(textarea);
  196.     ExitEasyGadget();        /* you MUST call ExitEasyGadget() before exiting */
  197.     exit();
  198. }
  199.  
  200.  
  201. main()
  202. {
  203.     struct IntuiMessage *message;
  204.     BOOL close_me;
  205.     COUNT i;
  206.  
  207.     /* Open the window */
  208.     window = (struct Window *) OpenWindow( &new_window );
  209.  
  210.     /* window open ? */
  211.     if(!window)
  212.         clean_exit();
  213.  
  214.     /* Init IObject.lib */
  215.     if (!InitEasyGadget())
  216.         clean_exit();
  217.  
  218.     /* create the TextArea, exit if error */
  219.     if (!(textarea = CreateTextArea(&newtextarea, window->RPort)))
  220.         clean_exit();
  221.  
  222.     /* Create objets */
  223.     EZGadget[ID_CYCLE]    = CreateCycle(&newcycle, ID_CYCLE);
  224.     EZGadget[ID_SCROLLER] = CreateScroller(&newscroller, ID_SCROLLER);
  225.     EZGadget[ID_BUTTON]   = CreateButton(&newbutton, ID_BUTTON);
  226.     EZGadget[ID_CHECK]    = CreateCheck(&newcheck, ID_CHECK);
  227.     EZGadget[ID_INTEGER]  = CreateInteger(&newint, ID_INTEGER);
  228.     EZGadget[ID_TGBUTTON] = CreateButton(&newtoggle, ID_TGBUTTON);
  229.     EZGadget[ID_STRING]   = CreateString(&newstr, ID_STRING);
  230.  
  231.     /* Verify if all were objects created */
  232.     for (i = ID_MIN; i <= ID_MAX; i++)
  233.         if (EZGadget[i] == NULL)
  234.             clean_exit();
  235.  
  236.     /* add object to window and display them */
  237.     for (i = ID_MIN; i <= ID_MAX; i++)
  238.         if (EZGadget[i])
  239.         {
  240.             AddObjectToWindow(EZGadget[i], window, NULL);
  241.             DisplayObject(EZGadget[i]);
  242.         }
  243.  
  244.     /* welcome message */
  245.     TAPuts(textarea, "Welcome in IObject.lib");
  246.  
  247.     /* fill the hot keys array for short-cut so with hot keys you can find object */
  248.     for (i = ID_MIN; i <= ID_MAX; i++)
  249.         HotKeyTable[i] = GetHotKey(EZGadget[i]);
  250.  
  251.     /* integer between -5000 and 5000 */
  252.     ModifyObject(EZGadget[ID_INTEGER], 0, -5000, 5000);
  253.  
  254.     /* loop until user click on closewindow gadget */
  255.     close_me = FALSE;
  256.     while(close_me == FALSE)
  257.     {
  258.         /* wait an event */
  259.         Wait(1 << window->UserPort->mp_SigBit);
  260.  
  261.         /* get IntuiMessage */
  262.         while(message = (struct IntuiMessage *) GetMsg(window->UserPort))
  263.         {
  264.             struct IntuiMessage msg;
  265.             BOOL changed;
  266.             CPTR obj;
  267.             UWORD ID;
  268.  
  269.             changed = FALSE;
  270.             msg = *message;            /* copy message */
  271.             ReplyMsg(message);
  272.  
  273.             /* process event */
  274.             switch( msg.Class )
  275.             {
  276.                 case CLOSEWINDOW:  /* CloseWindow gadget => go away */
  277.                     close_me=TRUE;
  278.                     break;
  279.  
  280.                 case VANILLAKEY:    /* short cut */
  281.                     if ((ID = FindHotKey(msg.Code, HotKeyTable, ID_MAX+1)) != -1)
  282.                     {
  283.                         obj = EZGadget[ID];
  284.                         ActivateObject(obj);
  285.                         changed = TRUE;
  286.                     }
  287.                     break;
  288.  
  289.                 default:
  290.                     if (obj = FindObjectMsg(&msg))
  291.                     {
  292.                         ID = GetObjectID(obj);
  293.                         changed = SendMsgToObject(obj, &msg);
  294.                     }
  295.                     break;
  296.             }
  297.  
  298.             if (changed)
  299.             {
  300.                 switch (ID)
  301.                 {
  302.                     case ID_CYCLE:
  303.                         TAPrintf(textarea, "Option %s selected.\n", text[ObjectValue(obj)]);
  304.                         break;
  305.                     case ID_SCROLLER:
  306.                         TAPrintf(textarea, "Position = %ld.\n", ObjectValue(obj));
  307.                         break;
  308.                     case ID_BUTTON:
  309.                         TAPuts(textarea, "Button pressed."); 
  310.                         break;
  311.                     case ID_CHECK:
  312.                         if (ObjectValue(obj))
  313.                             TAPuts(textarea, "Check is marked.");
  314.                         break;
  315.                     case ID_INTEGER:
  316.                         TAPrintf(textarea, "Integer Value is %ld.\n", ObjectValue(obj));
  317.                         break;
  318.                     case ID_STRING:
  319.                         TAPrintf(textarea, "You have entered the text: %s.\n", ObjectValue(obj));
  320.                         break;
  321.                     case ID_TGBUTTON:
  322.                         TAPrintf(textarea, "Button is %s.\n", ObjectValue(obj) ? "On" : "Off");
  323.                         break;
  324.                 }
  325.             }
  326.         }
  327.     }
  328.     clean_exit();
  329. }
  330.